package ym.controller.cost;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import ym.pojo.Cost;
import ym.service.CostService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 消费记录
 */
@CrossOrigin
@RestController
@RequestMapping("/front/cost")
public class FrontCostCon {
    @DubboReference
    private CostService costService;

    /**
     * 新增
     *
     * @param cost
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(Cost cost) {
        costService.add(cost);
        return BaseResult.ok();
    }


    /**
     * 修改
     *
     * @param cost
     * @return 执行结果
     */
    @PostMapping("/update")
    public BaseResult update(Cost cost) {
        costService.update(cost);
        return BaseResult.ok();
    }


    /**
     * 删除
     *
     * @param costId
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer costId) {
        costService.delete(costId);
        return BaseResult.ok();
    }


    /**
     * 根据id查询
     *
     * @param costId
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer costId) {
        Cost cost = costService.findById(costId);
        return BaseResult.ok(cost);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<Cost> all = costService.findAll();
        return BaseResult.ok(all);
    }

    /**
     * 分页查询
     *
     * @param search 搜索条件
     * @param page   当前页
     * @param size   每页大小
     * @return
     */
    @GetMapping("/search")
    public BaseResult search(String search, int page, int size) {
        Page<Cost> costPage = costService.search(search, page, size);
        return BaseResult.ok(costPage);
    }
}

